// 1.导入express框架
import express from "express";
import cors from "cors";

// 2.创建应用程序
const app = express();

// 3.创建对应的请求路由
// app.请求方法("请求路径", (req,res) => {})
app.use(cors());  //允许跨域

// 设置中间件接受请求体参数
app.use(express.json());
app.use(express.urlencoded({ extended: true }));


app.get("/user", (req, res) => {
  // res.json(接口数据):给前端响应接口数据
  let user = { name: "二桥", age: 21 }
  res.json(user)
});

app.post("/fruit", (req, res) => {
  // res.json(接口数据):给前端响应接口数据
  let data = ["apple", "banana", "orange", "pear", "durian"];
  res.json(data);
});

app.get("/songs", (req, res) => {
  // 接收查询字符串 req.query
  let { id } = req.query;
  // console.log(req.query);
  let datas = [
    { id: 2636746926, name: "不懂她", author: "伍六七" },
    { id: 2639639291, name: "不乱不破", author: "七八九" },
    { id: 11111, name: "两只老虎", author: "汪哥" },
  ];
  let result = datas.filter((item) => {
    return item.id === parseInt(id);
  });
  res.json(result[0] || {});
});

app.post("/login", (req, res) => {
  // 接受post请求体参数req.body
  let { username, password } = req.body;
  let users = [
    { id: 1, username: "erqiao", password: "228228" },
    { id: 2, username: "shiya", password: "112112" },
  ];

  // 匹配判断(过滤)
  let result = users.filter((user) => {
    return user.username === username && user.password === password;
  });

  if (result.length > 0) {
    res.json({ message: "登录成功", code: 20000 });
  } else {
    res.json({ message: "用户名或密码错误", code: 20001 });
  }
});

app.get("/news", (req, res) => {
  let datas = [
    {
      id: "1",
      title: "我校牵头成立全国金融科技行业产教融合共同体",
      content: "10月25日，由江西财经大学、北京济安金信科技有限公司、江西财经职业学院三方牵头发起的全国金融科技行业产教融合共同体在江西南昌正式成立，江西财经大学校长邓辉，江西省教育考试院党委书记刘彪文，中国人民大学金融信息中心主任、北京济安金信科技有限公司董事长杨健，深圳市中元产教融合科技有限公司董事长燕中元，江西财经职业学院校长彭慧出席成立大会并致辞，江西财经大学党委常委、副校长李春根主持会议。",
      pubDate: "2024-10-31",
      coverImg: "https://www.jxvc.jx.cn/__local/7/19/1C/CE0A1D9472548F4CA29BEAF9DDB_034488B1_3B1EF.png",
    },
    {
      id: "2",
      title: "铜陵市政府副市长杨文萍一行来我校考察调研",
      content: "10月29日下午，铜陵市人民政府副市长杨文萍带队来我校考察调研产教融合工作，九江市政府党组成员、副市长杜少华，校党委副书记、校长彭慧，校党委委员刘锋以及华为技术有限公司、江西赛创信息技术有限公司负责人等陪同调研。",
      pubDate: "2024-10-30",
      coverImg: "https://www.jxvc.jx.cn/__local/F/C2/CF/5201AEDDC051F1A9358C2CFACB2_FCDB6C8C_15C2C.png",
    }
  ];
  res.json(datas);
});

app.get("/tabs", (req, res) => {
  // 接受查询字符串
  let { id } = req.query;
  let datas = [
    { id: 1, title: "walk", content: "陪二桥散步" },
    { id: 2, title: "tea", content: "和二桥喝茶" },
    { id: 3, title: "read", content: "跟二桥读书" },
  ];
  // 匹配指定的id
  let result = datas.filter(item => {
    return item.id === parseInt(id);
  })

  if (result.length > 0) {
    res.json(result[0]);
  } else {
    res.json();
  };
});

app.get("/search", (req, res) => {
  let { word } = req.query;
  if (!word) {
    res.json([]);
    return;
  }
  let datas = [
    { id: "1", content: "html11111" },
    { id: "2", content: "HTML22222" },
    { id: "3", content: "css33333" },
    { id: "4", content: "CSS44444" },
    { id: "5", content: "javascript55555" },
    { id: "6", content: "JAVASCRIPT66666" },
  ];
  let result = datas.filter(item => {
    return item.content.toLowerCase().includes(word.toLowerCase());
  });
  res.json(result);
});
// 4.指定端口号，启动服务

app.get("/tab", (req, res) => {
  let datas = {
    title: [
      { id: 1, title: 'walk' },
      { id: 2, title: 'tea' },
      { id: 3, title: 'read' }
    ],
    content: [
      { id: 1, content: '跟二桥散步' },
      { id: 2, content: '和二桥喝茶' },
      { id: 3, content: '与二桥读书' }
    ]
  }
  res.json(datas);
})
app.listen(5000, () => {
  console.log("server is running at port 5000");
});

